package com.gw.ycny.common;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class MyDateUtil {
    static SimpleDateFormat sdfY = new SimpleDateFormat("yyyy");
    static SimpleDateFormat sdfYM = new SimpleDateFormat("yyyyMM");
    static SimpleDateFormat sdfYMD = new SimpleDateFormat("yyyyMMdd");
    static SimpleDateFormat sdfYMDHMS = new SimpleDateFormat("yyyyMMdd hh:mm:ss");

    public static Date yStrToDate(String yStr){
        Date date=null;
        try {
            date = sdfYM.parse(yStr);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    public static Date ymStrToDate(String ymStr){
        Date date=null;
        try {
            date = sdfYM.parse(ymStr);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    public static Date ymdStrToDate(String ymdStr){
        Date date=null;
        try {
            date = sdfYMD.parse(ymdStr);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static int getDaysOfMonth(String month) {
        Date date=null;
        try {
            date = sdfYM.parse(month);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    public static Date dateAddDays(Date date,int count) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE,count); //把日期往后增加一天,整数  往后推,负数往前移动
        date=calendar.getTime(); //这个时间就是日期往后推一天的结果
        return date;

    }

    /**
     * 获取今天yyyy-mm-dd格式时间
     * @return
     */
    public static Date getTodayDate() {
        Date date = new Date(System.currentTimeMillis());
        return ymdStrToDate(sdfYMD.format(date));
    }

    /**
     * Date转为"MM月dd日"格式字符串
     * @param date
     */
//    public static String dateToChieseStrMd(Date date) {
//        SimpleDateFormat sdfYMChiese = new SimpleDateFormat("MM月dd日");
//        return sdfYMChiese.format(date);
//    }
//    public static String strToChieseStrMd(Date date) {
//        SimpleDateFormat sdfYMChiese = new SimpleDateFormat("MM月dd日");
//        return sdfYMChiese.format(date);
//    }

    /**
     * Date转为"yyyy年MM月"格式字符串
     * @param date
     */
//    public static String dateToChieseStrYm(Date date) {
//        SimpleDateFormat sdfYMChiese = new SimpleDateFormat("yyyy年MM月");
//        return sdfYMChiese.format(date);
//    }
//    public static String dateToChieseStrYm(Date date) {
//        SimpleDateFormat sdfYMChiese = new SimpleDateFormat("yyyy年MM月");
//        return sdfYMChiese.format(date);
//    }

    public static Date dateAddYears(Date date, int i) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.YEAR,i);
        date=calendar.getTime();
        return date;
    }

    public static Date getNextMonth() {
        Date date = new Date(System.currentTimeMillis());
        date=dateAddMonths(date,1);
        return ymStrToDate(sdfYM.format(date));
    }

    public static Date dateAddMonths(Date date, int i) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.MONTH,1);
        return calendar.getTime();
    }
    public static Date getTodayYear() {
        Date date = new Date(System.currentTimeMillis());
        return yStrToDate(sdfY.format(date));
    }
    public static Date getTodayDay() {
        Date date = new Date(System.currentTimeMillis());
        return ymdStrToDate(sdfYMD.format(date));
    }

    public static String dateToStrYmd(Date date) {
        return sdfYMD.format(date);
    }
    public static String dateToStrYm(Date date) {
        return sdfYM.format(date);
    }
    public static String dateToStrY(Date date) {
        return sdfY.format(date);
    }

    public static Date getNowHour() {
        Date date = new Date(System.currentTimeMillis());
        return ymdStrToDate(sdfYMDHMS.format(date));
    }

    public static Date dateAddHours(Date date ,int i) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.HOUR,i);
        date=calendar.getTime();
        return date;

    }

    public static String dateToStrYmdhms(Date date) {
        return sdfYMDHMS.format(date);
    }
}
